<?php session_start();?>
<html>
<head>
<link href="install.css" rel="stylesheet" type="text/css" />

</head>
<body>
<?php switch($_GET['step']) {
	case '1':
	default:
		// Check for compatibility
		$phpCheck = FALSE;
		?>
		<h1>Welcome to Concerto.</h1>
		<h2>Checking compatibility...</h2>
		<?php
		$version = phpversion();
		if($version >= 5.3) $phpCheck = TRUE;
		$_SESSION['phpCheck'] = $phpCheck;
		if($phpCheck) : ?>
			<h3>Looks like your system can run Concerto.</h3>
			<a href="?step=2">Step 2 &rarr;</a>
		<?php else : ?>
			<h3>You need to have PHP version 5.3 or higher in order to run Concerto. Please contact your system administrator to upgrade.</h3>
		<?php endif;
		break;
	case '2':
		// Database Information
		?>
		<h1>MySQL Database Information</h1>
		<h3>At the moment, Concerto only supports MySQL databases.</h3>
		<form action="<?=$_SERVER['PHP_SELF']?>?step=3" method="POST">
			<label>Host (usually 'localhost')</label>
			<input type="text" name="db_host"/>
			<label>Username</label>
			<input type="text" name="db_user"/>
			<label>Password</label>
			<input type="text" name="db_password"/>
			<label>Database Name</label>
			<input type="text" name="db_name"/><br /><br />
			<input type="submit" value="Submit"/>
		</form>
		<?php
		break;
	case '3':?>
		<h1>Checking Database Connection...</h1>
		<?php
		// Validate Database Information
		$connect = mysql_connect($_POST['db_host'], $_POST['db_user'], $_POST['db_password']);
		$select = mysql_select_db($_POST['db_name']);
		$connected = TRUE;
		if($connect) : ?>
		<h3>Successfully connected to database server...</h3>
		<?php else : $connected = FALSE;?>
		<h3>Could not connect to database server.</h3>
		<?php endif;
		if($select) : ?>
		<h3>Successfully selected database.</h3>
		<?php else: $connected = FALSE;?>
		<h3>Could not select database.</h3>
		<?php endif;?>
		<?php if($connected) : 
			$_SESSION['db_host'] = $_POST['db_host'];
			$_SESSION['db_user'] = $_POST['db_user'];
			$_SESSION['db_password'] = $_POST['db_password'];
			$_SESSION['db_name'] = $_POST['db_name'];
		?>
		<a href="<?=$_SERVER['PHP_SELF']?>?step=4">Step 4 &rarr;</a>
		<?php else : ?>
		<a href="?step=2">&larr; Try Again</a>
		<?php endif;
		break;
	case '4':?>
		<h1>Site Information</h1>
		<form action="<?=$_SERVER['PHP_SELF']?>?step=5" method="POST">
			<label>Site Name</label>
			<input type="text" name="site_name"/>
			<label>Site URL (With http://, www if applicable, and trailing slash. E.g. "http://www.mysite.com/")</label>
			<input type="text" name="site_url"/>
			<?php $path = str_replace('admin/install.php', '', $_SERVER['SCRIPT_FILENAME']);?>
			<input type="submit" value="Finish &rarr;"/>
		</form>
		<?php
		break;
	case '5':
		$_SESSION['site_name'] = $_POST['site_name'];
		$_SESSION['site_url'] = $_POST['site_url'];
		$_SESSION['site_format'] = $_POST['site_format'];
		?>
		<h1>Validate Data</h1>
		<h2>Is all this information correct?</h2>
		<h3>Database Information</h3>
		<p><b>Host:</b> <?=$_SESSION['db_host'];?></p>
		<p><b>Username:</b> <?=$_SESSION['db_user'];?></p>
		<p><b>Password:</b> <?=$_SESSION['db_password'];?></p>
		<p><b>Database:</b> <?=$_SESSION['db_name'];?></p>
		<h3>Site Information</h3>
		<p><b>Site Name:</b> <?=$_POST['site_name'];?></p>
		<p><b>Site URL:</b> <?=$_POST['site_url'];?></p>
		<p><b>Site Format:</b> <?=$_POST['site_format'];?></p>
		<a href="<?=$_SERVER['PHP_SELF']?>">No, start over.</a>
		<a href="<?=$_SERVER['PHP_SELF']?>?step=6">Yes, begin installation.</a>
		<?php
		break;
	case '6':
		// Generate cn-config.php file
		
		$root = str_replace('install/index.php', '', $_SERVER['SCRIPT_FILENAME']);
		$url = $_SESSION['site_url'];
		if(substr($url, -1) != '/') $url .= '/';
		$port = 'FALSE';
		if(strpos($url, ':') !== FALSE) {
			$port = 'TRUE';
		}
		
		$filename = "../cn-config.php";
		$handle = fopen($filename, 'w') or die("can't open file");
		$content = "<?php
define('CN_DB_HOST', '".$_SESSION['db_host']."');
define('CN_DB_USER', '".$_SESSION['db_user']."');
define('CN_DB_PASSWORD', '".$_SESSION['db_password']."');
define('CN_DB_NAME', '".$_SESSION['db_name']."');
define('CN_SITE_NAME', '".$_SESSION['site_name']."');
define('CN_URL', '".$url."');
define('CN_ROOT_URL', '".$url."');
define('CN_APPLICATION_PATH', '".$root."concerto/');
define('CN_URL_PORT', ".$port.");
?>";
		fwrite($handle, $content);
		fclose($handle);
		
		// Write htaccess file
		$filename = "../.htaccess";
		$handle = fopen($filename, 'a') or die("can't open file");
		$content = '';
		if(strstr('www', $url)) {
			$content += '# Rewrite to www.
RewriteCond %{HTTP_HOST} ^'.str_replace('www.', str_replace('.', '\.', $url)).'$ [NC]
RewriteRule ^(.*)$ '.CN_URL.'$1 [L,R=301]';
		}
		
		// Get relative path from URL base
		$pos = strpos(str_replace('http://', '', $url), '/');
		$relativeUrl = substr(str_replace('http://', '', $url), $pos);
		$content .= '<IfModule mod_rewrite.c> 
		RewriteEngine On 
		RewriteBase / 
		RewriteCond %{REQUEST_FILENAME} !-f 
		RewriteCond %{REQUEST_FILENAME} !-d 
		RewriteRule . '.$relativeUrl.'index.php [L] 
		</IfModule>
';
		fwrite($handle, $content);
		fclose($handle);
		?>
		<h1>Install Concerto</h1>
		<form action="<?=$_SERVER['PHP_SELF']?>?step=7" method="post">
			<label>Administrator's E-mail:</label>
			<input type="text" name="email" size="100"/>
			<p><i>Default password is "admin". You can change this later.</i></p>
			<p><input type="submit" value="Install"/></p>
		</form>
		<?php
		break;
	case '7': //install db tables
		require '../cn-config.php';
		require 'cn-db-info.php';
		$connect = mysql_connect(CN_DB_HOST, CN_DB_USER, CN_DB_PASSWORD);
		$select = mysql_select_db(CN_DB_NAME);
		foreach($cn_db_tables as $table=>$sql) {
			echo "<p>Creating table `$table`...</p>";
			mysql_query($sql) or die(mysql_error());
		}
		insertDefaultFields();
		/** Add administrator */
		$password = md5('admin');
		$email = $_POST['email'];
		mysql_query("INSERT INTO `cn_users` (`user_email`, `user_password`, `user_level`) VALUES('$email', '$password', '2')");
		$version = file_get_contents(CN_APPLICATION_PATH.'application/version.txt');
		mysql_query("INSERT INTO `cn_options` (`option_name`, `option_value`) VALUES('template', 'default')");
		mysql_query("INSERT INTO `cn_options` (`option_name`, `option_value`) VALUES(`current_version', '2.1')");
		
		$_SESSION['fresh_install'] = TRUE;
		echo '<h2>Concerto installed! Click <a href="'.CN_URL.'admin/">here</a> to login and go to the admin panel.</h2>';
		break;
		
}
?>
</body>
</html>